<template>
    <div>
        <h1>订单列表</h1>
        <div>
            <el-form
                :inline="true"
                :model="searchForm"
                class="demo-form-inline"
            >
                <el-form-item label="关键字">
                    <el-input
                        v-model="searchForm.keyWord"
                        placeholder="请输入关键字"
                        clearable
                    />
                </el-form-item>
                <el-form-item label="订单状态">
                    <el-select
                        v-model="searchForm.status"
                        placeholder="请选择订单的状态"
                        style="width: 180px"
                        clearable
                    >
                        <el-option
                            v-for="item in statusList"
                            :label="item.dictLabel"
                            :value="item.dictValue"
                            :key="item.dictId"
                        />
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="onQuery">查询</el-button>
                    <el-button type="primary" @click="onBuy">购买</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div>
            <el-table :data="tableData" stripe style="width: 100%">
                <el-table-column prop="orderId" label="ID" width="100" />
                <el-table-column prop="orderCode" label="编号" width="220" />
                <el-table-column prop="amount" label="金额" width="100" />
                <el-table-column
                    prop="createTime"
                    label="下单时间"
                    width="180"
                />
                <el-table-column prop="nickName" label="消费者" width="120" />
                <el-table-column
                    prop="prodCount"
                    label="购买数量"
                    width="120"
                />
                <el-table-column
                    prop="orderStatus"
                    label="订单状态"
                    width="180"
                    :formatter="formatter"
                />
                <el-table-column label="操作" />
            </el-table>
        </div>
        <div style="margin-top: 20px">
            <el-pagination
                v-model:current-page="searchForm.pageNum"
                v-model:page-size="searchForm.pageSize"
                :page-sizes="[2, 4, 6, 8]"
                :background="true"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            />
        </div>
        <div>
            <el-dialog
                v-model="dialogFormVisible"
                title="购买商品"
                width="1000"
            >
                <div>共计: {{ buyAmount }} 元</div>
                <el-table :data="prodData" stripe style="width: 100%" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column prop="prodId" label="ID" width="100" />
                    <el-table-column prop="prodName" label="名称" width="180" />
                    <el-table-column prop="prodPrice" label="价格" width="100" />
                    <el-table-column prop="typeName" label="类型" width="120" />
                    <el-table-column prop="nickName" label="卖家" width="120" />
                    <el-table-column label="购买数量" width="180">
                        <template #default="scope">
                            <el-input-number v-model="scope.row.buyNum" :min="1" :max="10" @change="handleNumChange" />
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" />
                </el-table>
                <div style="margin-top: 20px;">
                    <el-pagination
                        v-model:current-page="prodForm.pageNum"
                        v-model:page-size="prodForm.pageSize"
                        :page-sizes="[2, 4, 6, 8]"
                        :background="true"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="prodTotal"
                        @size-change="handleSizeChangeProd"
                        @current-change="handleCurrentChangeProd"
                    />
                </div>
                <template #footer>
                    <div class="dialog-footer">
                        <el-button @click="dialogFormVisible = false"
                            >放弃</el-button
                        >
                        <el-button
                            type="primary"
                            @click="onSubmitBuy"
                        >
                            购买
                        </el-button>
                    </div>
                </template>
            </el-dialog>
        </div>
    </div>
</template>

<script setup lang="ts">
import { getOrderList, getDictList,getProdList,addOrder } from "@/api/api";
import { ItemStyleMixin } from "echarts/types/src/model/mixin/itemStyle.js";
import { onMounted, ref } from "vue";
import { ElMessage, type TableColumnCtx } from "element-plus";

const tableData = ref([]);
const total = ref(0);
const searchForm = ref({
    pageNum: 1,
    pageSize: 2,
    keyWord: null,
    status: null,
});
const statusList = ref([]);

const dialogFormVisible = ref(false)
const formLabelWidth = '140px'
const prodData = ref([])
const prodTotal = ref(0)
const prodForm = ref({
    pageNum: 1,
    pageSize: 2,
})
const multipleSelection = ref<any[]>([])
const buyAmount = ref(0.0)


// 下单
const onSubmitBuy = () => {
    addOrder(multipleSelection.value).then(res => {
        if(res.data.code == 200) {
            ElMessage.success('购买成功');
            dialogFormVisible.value = false;
            loadList();
        }
        else {
            ElMessage.error(res.data.msg);
        }
    })
}



const handleNumChange = (value: number | undefined) => {
  console.log(value);

  if(multipleSelection.value.length > 0) {
    buyAmount.value = 0.00;
    multipleSelection.value.forEach(item => {
        buyAmount.value += (item.prodPrice*item.buyNum);
    })
  }
  else {
    buyAmount.value = 0.00;
  }
}

// 选择商品事件
const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val;
  console.log('handleSelectionChange', multipleSelection.value);
  if(multipleSelection.value.length > 0) {
    buyAmount.value = 0.00;
    multipleSelection.value.forEach(item => {
        buyAmount.value += (item.prodPrice*item.buyNum);
    })
  }
  else {
    buyAmount.value = 0.00;
  }
}


const handleSizeChangeProd = (val: number) => {
  console.log(`${val} items per page`);
  prodForm.value.pageSize = val;
  loadProdList();
}
const handleCurrentChangeProd = (val: number) => {
  console.log(`current page: ${val}`)
  prodForm.value.pageNum = val;
  loadProdList();
}


const onQuery = () => {
    console.log("onQuery");
    loadList();
};

const loadProdList = () => {
    // 获取商品分页列表
    getProdList(prodForm.value).then(res => {
        if(res.data.code == 200) {
            prodData.value = res.data.data.records;
            prodTotal.value = res.data.data.total;
        }
    })
}

// 显示购买对话框
const onBuy = () => {
    console.log("onBuy");

    loadProdList();

    // 显示对话框
    dialogFormVisible.value = true;
};

const formatter = (row: any, column: TableColumnCtx<any>) => {
    let name = "未知";
    statusList.value.forEach((item) => {
        if (item.dictValue == row.orderStatus) {
            name = item.dictLabel;
        }
    });
    return name;
};

const handleSizeChange = (val: number) => {
    console.log(`${val} items per page`);
    searchForm.value.pageSize = val;
    loadList();
};
const handleCurrentChange = (val: number) => {
    console.log(`current page: ${val}`);
    searchForm.value.pageNum = val;
    loadList();
};

// 分页获取订单列表
const loadList = () => {
    getOrderList(searchForm.value).then((res) => {
        if (res.data.code == 200) {
            tableData.value = res.data.data.records;
            total.value = res.data.data.total;
        }
    });
};

// 页面加载钩子函数中
onMounted(() => {
    loadList();

    // 获取订单状态
    getDictList(3).then((res) => {
        if (res.data.code == 200) {
            statusList.value = res.data.data;
        }
    });
});
</script>

<style scoped></style>
